"""
Case Type   : 硬件故障类--network
Case Name   : 集群重启时，备节点网卡down
Create At   : 2021/08/02
Owner       : @zou_jialiang0505328126
Description :
    1.停止集群
    2.注入网卡down故障
    3.重启集群
    4.注入网卡down故障
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
History     :
"""

import unittest
import time
from yat.test import Node
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.CommonSH import RestartDbCluster
from testcase.utils.Logger import Logger
from testcase.utils.ComThread import ComThread


class NetWork(unittest.TestCase):
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info(
            '---------Opengauss_Reliability_Hardware_Case110:开始执行---------')
        self.primary_root_node = Node(node='PrimaryRoot')
        self.sta1_root_node = Node(node='Standby1Root')
        self.sta1_dbuser = Node(node='Standby1DbUser')
        self.com = Common()
        self.constant = Constant()
        self.primary_sh = CommonSH('PrimaryDbUser')

        self.log.info("-------------获取网卡名---------------")
        network_card_cmd = 'ifconfig'
        self.log.info(network_card_cmd)
        network_card_result = self.sta1_root_node.sh(
            network_card_cmd).result()
        network_card_list = network_card_result.split('\n\n')
        self.log.info(network_card_list)
        self.networkcard = ''
        for network_card_item in network_card_list:
            if self.sta1_dbuser.db_host in network_card_item:
                self.networkcard = network_card_item.split(':')[0].strip()
                self.log.info(self.networkcard)

    def test_main(self):
        self.log.info("------------1.停止集群----------------")
        stop_thread = ComThread(self.primary_sh.stopDbCluster, args=())
        stop_thread.setDaemon(True)
        stop_thread.start()

        self.log.info("--------------2.注入网卡down故障-------------")
        time.sleep(1)
        inject_command = f"rNet_down (dev, time)" \
            f"values({self.networkcard}, 30)"
        cfe_thread = ComThread(
            self.com.cfe_inject, args=(self.sta1_root_node,
                                       inject_command,))
        cfe_thread.setDaemon(True)
        cfe_thread.start()

        self.log.info("---获取故障结果:网卡中断无法获取结果，此处获取为保证故障时间已到--")
        cfe_thread.join(180)
        result = cfe_thread.get_result()
        self.log.info(result)

        self.log.info("-----------------获取停止结果-------------")
        text = '----step1：停止集群 expect:成功----'
        stop_thread.join(180)
        stop_result = stop_thread.get_result()
        self.log.info(stop_result)
        time.sleep(25)
        result = self.primary_sh.getDbClusterStatus("status")
        self.assertFalse(result, '执行失败:' + text)

        self.log.info("------------3. 启动集群----------------")
        start_thread = ComThread(self.primary_sh.startDbCluster, args=())
        start_thread.setDaemon(True)
        start_thread.start()

        self.log.info("--------------4. 注入网卡down故障-------------")
        time.sleep(1)
        inject_command = f"rNet_down (dev, time)" \
            f"values({self.networkcard}, 30)"
        cfe_thread = ComThread(
            self.com.cfe_inject, args=(self.sta1_root_node,
                                       inject_command,))
        cfe_thread.setDaemon(True)
        cfe_thread.start()

        self.log.info("-------------------获取故障结果---------------------")
        cfe_thread.join(180)
        result = cfe_thread.get_result()
        self.log.info(result)

        self.log.info("-----------------获取启动结果-------------")
        text = '----step3：启动集群 expect:成功----'
        start_thread.join(180)
        start_result = start_thread.get_result()
        self.log.info(start_result)
        time.sleep(25)
        result = self.primary_sh.getDbClusterStatus("status")
        self.assertTrue(result)
        self.assertTrue(start_result and result, '执行失败:' + text)

    def tearDown(self):
        time.sleep(25)
        self.log.info(
            '--------Opengauss_Reliability_Hardware_Case110:执行完成--------')
